package com.camartsApp.service.impl;


import com.camartsApp.Util.RedisUtil;
import com.camartsApp.service.UserRelationshipService;
import com.camartsApp.Util.Response;
import com.camartsApp.common.ResultVO;
import com.camartsApp.entity.UserRelationship;

import com.camartsApp.mapper.UserInfoMapper;
import com.camartsApp.mapper.UserRelationshipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserRelationshipServiceImpl implements UserRelationshipService {

    @Autowired
    private UserInfoMapper userMapper;

    @Autowired
    private UserRelationshipMapper relationshipMapper;

    @Autowired
    private RedisUtil redisUtil;  // 注入RedisUtil

    private static final String TOP_USER_CACHE_KEY = "cache:topUsersWithPhoto";

    @Transactional
    @Override
    public ResultVO follow(Long followerId, Long followingId) {
        if (followerId.equals(followingId)) {
            return Response.FailResultByMsg("不能关注自己");
        }

        UserRelationship existing = relationshipMapper.findByFollowerAndFollowing(followerId, followingId);
        if (existing != null && existing.getStatus() == 1) {
            return Response.FailResultByMsg("已经关注");
        }

        if (existing == null) {
            UserRelationship relationship = new UserRelationship();
            relationship.setFollowerId(followerId);
            relationship.setFollowingId(followingId);
            relationship.setStatus(1);
            relationshipMapper.insert(relationship);
        } else {
            relationshipMapper.updateStatus(followerId, followingId, 1);
        }

        // 分别更新关注数和粉丝数
        userMapper.updateFollowingCount(followerId, 1);   // 用户A的关注数+1
        userMapper.updateFollowerCount(followingId, 1);   // 用户B的粉丝数+1

        // 点这里清除缓存
        redisUtil.delete(TOP_USER_CACHE_KEY);
        return Response.OkByMsg("关注成功");
    }

    @Transactional
    @Override
    public ResultVO unfollow(Long followerId, Long followingId) {
        UserRelationship existing = relationshipMapper.findByFollowerAndFollowing(followerId, followingId);
        if (existing == null || existing.getStatus() == 0) {
            return Response.FailResultByMsg("未关注");
        }

        relationshipMapper.updateStatus(followerId, followingId, 0);

        // 分别更新关注数和粉丝数
        userMapper.updateFollowingCount(followerId, -1);  // 用户A的关注数-1
        userMapper.updateFollowerCount(followingId, -1);  // 用户B的粉丝数-1


        // 点这里清除缓存
        redisUtil.delete(TOP_USER_CACHE_KEY);
        return Response.OkByMsg("取消关注");
    }

    @Override
    public ResultVO getFollowingListByUserId(Long userId) {
        List<Long> followingIds = relationshipMapper.selectFollowingIdsByFollowerId(userId);
        return Response.OkByData(followingIds);
    }

    @Override
    public ResultVO getFollowerListByUserId(Long userId) {
        List<Long> followerIds = relationshipMapper.selectFollowerIdsByFollowingId(userId);
        return Response.OkByData(followerIds);
    }

    @Override
    public ResultVO checkIsFollowing(Long currentUserId, Long targetUserId) {
        if (currentUserId == null || targetUserId == null) {
            return Response.FailResultByMsg("参数错误");
        }

        int count = relationshipMapper.isFollowing(currentUserId, targetUserId);
        boolean isFollowing = count > 0;

        return Response.OkByData(isFollowing);
    }



}
